/**
 * Created by dfsj16111702 on 2016/12/27.
 */
angular.module('mamApp').controller('albumPlayDetailController', ['$scope', 'AlbumPlayManager', '$state','api','paramsManager',
    function ($scope, AlbumPlayManager, $state,api,paramsManager) {
        $scope.albumName=$state.params.albumName

        $scope.search = {
            pageNo: $state.params.pageNo || 1,
            pageSize: $state.params.pageSize || 10,
            albumId:$state.params.albumId,
            startTime: $state.params.startTime || "",
            endTime: $state.params.endTime || "",
            "X-Auth-Token":$scope.userInfo.token
        };

        $scope.paginationConf = {
            currentPage: $state.params.pageNo || 1,
            itemsPerPage: $state.params.pageSize || 10,
            pagesLength: 9,
            search: 0,
            onChange: function () {
                $scope.search.pageNo = this.currentPage;
                $scope.search.pageSize = this.itemsPerPage;
                $state.go($state.current.name, $scope.search)
            }
        };

        $scope.downHref=api+"/cp/stat/play/albums/"+$scope.search.albumId+"/excel"+paramsManager.query($scope.search);

        $scope.rangeDate={
            startDate: $scope.search.startTime ? moment($scope.search.startTime).format('X') : null,
            endDate: $scope.search.endTime ? moment($scope.search.endTime).format('X') : null,
            onSelected: function (data, scope) {  //时间选择确认事件
                $scope.search.startTime = data.startDate ? moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD") : null;
                $scope.search.endTime = data.endDate ? moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD") : null;
                $scope.search.pageNo=1;
                $state.go($state.current.name, $scope.search);

            }
        }

        AlbumPlayManager.albumPlayDetail([$scope.search.albumId,$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
            $scope.albumPlayDetailList = result.data.pageList;
            $scope.albumPlayDetailAllList=result.data.totalCount;
            $scope.paginationConf.totalItems = result.data.total;
            var data=angular.copy($scope.albumPlayDetailList).reverse();
            var xAxisData=[];
            var playNum=[];
            var downloadNum=[];
            var onlinePlayNum=[];
            var offlinePlayNum=[];
            data.forEach(function (item) {
                xAxisData.push(item.countTime);
                playNum.push(item.playNum);
                downloadNum.push(item.downloadNum);
                onlinePlayNum.push(item.onlinePlayNum);
                offlinePlayNum.push(item.offlinePlayNum);
            });
            var seriesData=[
                {name:"播放总量",data:playNum, type:'line'},
                {name:"在线播放量",data:onlinePlayNum, type:'line'},
                {name:"离线播放量",data:offlinePlayNum, type:'line'},
                {name:"下载量",data:downloadNum, type:'line'}
            ];
            programsOverviewOptInit(xAxisData,seriesData);
        })
        function programsOverviewOptInit(xAxisData,seriesData) {
            $scope.programsOverviewOpt = {
                title: {
                    text: $scope.albumName
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data:['播放总量','在线播放量','离线播放量','下载量'],
                    bottom:"-5"
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '6%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: xAxisData
                },
                yAxis: {
                    type: 'value'
                },
                series:seriesData
            };
        }

    }]);
